package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.NextAdapter3;
import com.weiyi.mashangzhu.Landlord.Bean.SetHouseBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditHouseActivity extends BaseActivity {

    @BindView(R.id.lv)
    MyListView lv;
    List<SetHouseBean> list = new ArrayList<>();
    ArrayList<String> photos = new ArrayList<>();
    ArrayList<String> photos2 = new ArrayList<>();
    List<String> photoslist = new ArrayList<>();
    @BindView(R.id.photo_yes)
    ImageView photoYes;
    @BindView(R.id.photo_count)
    TextView photoCount;
    @BindView(R.id.sv)
    ScrollView sv;
    NextAdapter3 nextAdapter3 = null;
    String house_id;
    boolean flag = true;
    List<String> photolist = new ArrayList<>();
    String img1, img2, img3, img4, img5, img6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_house);
        ButterKnife.bind(this);
        sv.smoothScrollTo(0, Integer.MAX_VALUE);
        house_id = getIntent().getStringExtra("id");
        MyApp.images.clear();
        initPhoto();

        init();
        list.add(new SetHouseBean("撰写房屋标题和房屋介绍", ""));
        list.add(new SetHouseBean("设置房屋折扣", ""));
        list.add(new SetHouseBean("设置房屋价格", ""));
        list.add(new SetHouseBean("基本信息", ""));
        list.add(new SetHouseBean("账户信息", ""));
        list.add(new SetHouseBean("认证材料", ""));
        list.add(new SetHouseBean("可选配套设施", ""));
        list.add(new SetHouseBean("房源类型", ""));
        list.add(new SetHouseBean("地理位置", ""));
        nextAdapter3 = new NextAdapter3(list, this, "img_have");
        lv.setAdapter(nextAdapter3);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        intent = new Intent(EditHouseActivity.this, SetHouseTitieActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(EditHouseActivity.this, SetDiscountActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 2:
                        intent = new Intent(EditHouseActivity.this, SetPriceActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 3:
                        intent = new Intent(EditHouseActivity.this, BaseInfoActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 4:
                        intent = new Intent(EditHouseActivity.this, AccountInfoActivity_L.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 5:
                        intent = new Intent(EditHouseActivity.this, ApproveActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 6:
                        intent = new Intent(EditHouseActivity.this, FacilityActivity_L.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 7:
                        intent = new Intent(EditHouseActivity.this, HouseTypeActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;
                    case 8:
                        intent = new Intent(EditHouseActivity.this, AdressActivity.class);
                        intent.putExtra("info","edit");
                        intent.putExtra("id",house_id);
                        EditHouseActivity.this.startActivity(intent);
                        break;

                }
            }
        });

    }
//!jsonobject2.getString("hImg1").equals("") &&
    private void initPhoto() {
        LoadingDialog.show(this);
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("id", house_id + "")
                .url(API.BaseApi_L + API.GetHouseImg)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        LoadingDialog.dismiss(EditHouseActivity.this);
                        T.showShort(EditHouseActivity.this, "网络连接失败");
                    }

                    @Override
                    public void onResponse(String response) {
                        LoadingDialog.dismiss(EditHouseActivity.this);
                        Log.e("eeeeeeeee", "===" + response);
                        try {
                            JSONObject jsonobject = new JSONObject(response);
                            if (jsonobject.getInt("status") == 0) {
                                JSONArray jsonarray = jsonobject.getJSONArray("data");
                                JSONObject jsonobject2 = jsonarray.getJSONObject(0);
                                GlideImgManager.glideLoader(EditHouseActivity.this, API.BaseApi_url + jsonobject2.getString("hImg1"), R.mipmap.errer, photoYes);
                                int count = 0;
                                if (!jsonobject2.getString("hImg1").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg1"));
                                    count += 1;
                                }
                                if ( !jsonobject2.getString("hImg2").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg2"));
                                    count += 1;
                                }
                                if ( !jsonobject2.getString("hImg3").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg3"));
                                    count += 1;
                                }
                                if (!jsonobject2.getString("hImg4").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg4"));
                                    count += 1;
                                }
                                if ( !jsonobject2.getString("hImg5").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg5"));
                                    count += 1;
                                }
                                if ( !jsonobject2.getString("hImg6").equals("null")) {
                                    Log.e("edddddddd",jsonobject2.getString("hImg6"));
                                    count += 1;
                                }
                                Log.e("edddddddd","size="+count);
                                photoCount.setText(count + "");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0x002) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(0).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x003) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(1).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x004) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(2).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x005) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(3).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x006) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(4).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x007) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(5).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x008) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(6).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x009) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(7).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
        if (resultCode == 0x0010) {
            String info = data.getStringExtra("info");
            if (info.equals("完成")) {
                list.get(8).setFinish("已完成");
                nextAdapter3.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPhoto();
    }
    private void showNormalDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setIcon(R.mipmap.tishi);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要删除此房源吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LoadingDialog.show(EditHouseActivity.this);
                        OkHttpUtils.get().addParams("token", SPUtils.get(EditHouseActivity.this, "token", "") + "")
                                .addParams("id",house_id)
                                .url(API.BaseApi_L+API.DeleteHouse)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(EditHouseActivity.this);
                                        T.showShort(EditHouseActivity.this, "数据获取失败，请稍后再试");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("tyyyyyyy","re="+response);
                                        LoadingDialog.dismiss(EditHouseActivity.this);
                                        try {
                                            JSONObject jsonObject=new JSONObject(response);
                                            if (jsonObject.getInt("status")==0){
                                               finish();
                                            }
                                            T.showShort(EditHouseActivity.this,jsonObject.getString("msg"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }
    @OnClick({R.id.back, R.id.delete, R.id.add_photo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.delete:
                //删除房源
showNormalDialog();
                break;
            case R.id.add_photo:

                Intent intent=new Intent(this, PhotoSelectActivity.class);
                intent.putExtra("id",house_id);
                intent.putExtra("info","edit");
                startActivity(intent);
                break;
        }
    }


//    @OnClick({R.id.back, R.id.add_photo, R.id.submit})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.back:
//                finish();
//                break;
//            case R.id.add_photo:
//                UiUtil.startActivity(this, PhotoSelectActivity.class);
//                break;
//            case R.id.submit:
//                if (MyApp.img1 == null) {
//                    T.showShort(this, "最少上传4张房源照片");
//                } else {
////
//                    if (list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            && list.get(0).getFinish().equals("已完成")
//                            ) {
//                        LoadingDialog.show(EditHouseActivity.this);
//                        OkHttpUtils.get()
//                                .addParams("token", SPUtils.get(this, "token", "") + "")
//                                .addParams("hTitle", MyApp.house_title)
//                                .addParams("hIntro", MyApp.house_content)
//                                .addParams("hPrice", MyApp.price_price)
//                                .addParams("hdeposit", MyApp.price_yajian)
//                                .addParams("hImg1", MyApp.img1)
//                                .addParams("hImg2", MyApp.img2)
//                                .addParams("hImg3", MyApp.img3)
//                                .addParams("hImg4", MyApp.img4)
//                                .addParams("hImg5", MyApp.img5)
//                                .addParams("hImg6", MyApp.img6)
//                                .addParams("hAddpeople", MyApp.price_add)
//                                .addParams("hOtherprice", MyApp.price_other)
//                                .addParams("hWedate", MyApp.dis_date)
//                                .addParams("hWeprice", MyApp.dis_price)
//                                .addParams("hSevendiscount", MyApp.dis_7)
//                                .addParams("hThirtydiscount", MyApp.dis_30)
//                                .addParams("hType", MyApp.type)
//                                .addParams("hArea", MyApp.base_area)
//                                .addParams("hRooms", MyApp.base_type_chu)
//                                .addParams("hRoom", MyApp.base_type_shi)
//                                .addParams("hHall", MyApp.base_type_ting)
//                                .addParams("hToilet", MyApp.base_type_wei)
//                                .addParams("hToilettype", MyApp.base_wei)
//                                .addParams("hPeoples", MyApp.base_person)
//                                .addParams("hBedtype", MyApp.base_bed_type)
//                                .addParams("hBedlength", MyApp.base_bed_l)
//                                .addParams("hBedwidth", MyApp.base_bed_w)
//                                .addParams("hBednum", MyApp.base_bed_count)
//                                .addParams("hRealname", MyApp.realname)
//                                .addParams("hMatches", MyApp.facility)
//                                .addParams("hIdcardnum", MyApp.idcard)
//                                .addParams("hIdpic1", MyApp.id1)
//                                .addParams("hIdpic2", MyApp.id2)
//                                .addParams("hIdpic3", MyApp.id3)
//                                .addParams("hReceivables", MyApp.account)
//                                .addParams("hProvince", MyApp.address_province)
//                                .addParams("hCity", MyApp.address_city)
//                                .addParams("hDistrict", MyApp.address_district)
//                                .addParams("hAddress", MyApp.address_address)
//                                .addParams("hBuilding", MyApp.address_loudong)
//                                .addParams("hFloor", MyApp.address_louceng)
//                                .addParams("hRoomnum", MyApp.address_roomnum)
//                                .addParams("hPhone", MyApp.phone)
//                                .url(API.BaseApi_L + API.AddHouse_Api)
//                                .build()
//                                .execute(new StringCallback() {
//                                    @Override
//                                    public void onError(Request request, Exception e) {
//                                        LoadingDialog.dismiss(EditHouseActivity.this);
//                                        T.showShort(EditHouseActivity.this, "网络连接失败，请检查一下网络设置");
//                                    }
//
//                                    @Override
//                                    public void onResponse(String response) {
//                                        LoadingDialog.dismiss(EditHouseActivity.this);
//                                        try {
//                                            JSONObject jsonObject = new JSONObject(response);
//                                            T.showShort(EditHouseActivity.this, jsonObject.getString("msg"));
//                                            if (jsonObject.getInt("status") == 0) {
//                                                finish();
//                                            }
//
//                                        } catch (JSONException e) {
//
//                                        }
//
//                                    }
//                                });
//
//                    } else {
//                        T.showShort(this, "请完善资料后提交审核");
//                    }
//                    break;
//                }
//        }
//    }
}
